package priv.stones.controller.administrate;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import priv.stones.common.annotation.CommitLog;
import priv.stones.common.utils.Result;
import priv.stones.entity.SysMenu;
import priv.stones.service.SysMenuService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Stones
 * @Description:
 * @Date: 2017/10/12 14:50
 */
@Controller
@RequestMapping("/administrate/menu")
public class MenuController extends BaseController {

    @Autowired
    protected SysMenuService sysMenuService;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping("")
    @RequiresPermissions("sys:menu:list")
    public String List(Model model
            , @RequestParam(value = "category", defaultValue = "1") int category
            , @RequestParam(value = "pageNo", defaultValue = "1") int pageNo
            , @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        EntityWrapper<SysMenu> ew = new EntityWrapper<>();
        ew.eq("category", category);
        ew.orderBy("sort", true);
        Page<SysMenu> data = sysMenuService.selectPage(new Page<>(pageNo, pageSize), ew);
        for (SysMenu sysMenu : data.getRecords()) {
            if (!StringUtils.isBlank(sysMenu.getParentid())) {
                sysMenu.setParentName(sysMenuService.selectById(sysMenu.getParentid()).getName());
            }
        }
        model.addAttribute("data", data.getRecords());
        model.addAttribute("category", category);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("dataCount", data.getTotal());
        return FreeMarkerViewResult("menu/list", model);
    }

    /**
     * 编辑
     *
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String Edit(Model model, @RequestParam(value = "id", defaultValue = "") String id) {
        SysMenu sysMenu;
        if (!StringUtils.isBlank(id)) {
            sysMenu = sysMenuService.selectById(id);
        } else {
            sysMenu = new SysMenu();
            sysMenu.setCategory(1);
            sysMenu.setStatus(1);
        }
        model.addAttribute("data", sysMenu);
        return FreeMarkerViewResult("menu/edit", model);
    }

    @RequestMapping(value = "/edit/insert", method = RequestMethod.POST)
    @RequiresPermissions("sys:menu:insert")
    @CommitLog("添加菜单")
    @ResponseBody
    public Result Insert(SysMenu sysMenu) {
        if (StringUtils.isBlank(sysMenu.getParentid())) {
            sysMenu.setParentid(null);
        }
        if (StringUtils.isBlank(sysMenu.getPrivilegemark())) {
            sysMenu.setPrivilegemark(null);
        }
        sysMenu.setCreatetime(new Date());
        return sysMenuService.insert(sysMenu) ? Result.Ok() : Result.Error();
    }

    @RequestMapping(value = "/edit/update", method = RequestMethod.POST)
    @RequiresPermissions("sys:menu:update")
    @CommitLog("修改菜单")
    @ResponseBody
    public Result Update(SysMenu sysMenu) {
        if (StringUtils.isBlank(sysMenu.getParentid())) {
            sysMenu.setParentid(null);
        }
        if (StringUtils.isBlank(sysMenu.getPrivilegemark())) {
            sysMenu.setPrivilegemark(null);
        }
        return sysMenuService.updateById(sysMenu) ? Result.Ok() : Result.Error();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @RequiresPermissions("sys:menu:delete")
    @CommitLog("删除菜单")
    @ResponseBody
    public Result Delete(String id) {
        SysMenu menu = sysMenuService.selectById(id);
        List<String> ids = new ArrayList<>();
        ids.add(menu.getId());
        List<SysMenu> chirlds = sysMenuService.selectList(new EntityWrapper<SysMenu>().eq("parentid", menu.getId()));
        for (SysMenu chirld : chirlds) {
            ids.add(chirld.getId());
            List<SysMenu> chirldChirlds = sysMenuService.selectList(new EntityWrapper<SysMenu>().eq("parentid", chirld.getId()));
            for (SysMenu chirldChirld : chirldChirlds) {
                ids.add(chirldChirld.getId());
            }
        }
        sysMenuService.deleteBatchIds(ids);
        return Result.Ok();
    }

    /**
     * 得到默认排序
     *
     * @param category
     * @return
     */
    @RequestMapping("/edit/getsort")
    @ResponseBody
    public Result GetSort(int category) {
        int count = sysMenuService.selectCount(new EntityWrapper<SysMenu>().eq("category", category));
        Result result = Result.Ok();
        result.put("sort", count + 10);
        return result;
    }

    /**
     * 得到父级
     *
     * @param category
     * @return
     */
    @RequestMapping("/edit/getparents")
    @ResponseBody
    public Result GetParents(int category) {
        category = category - 1;
        List<SysMenu> data = sysMenuService.selectList(new EntityWrapper<SysMenu>().eq("category", category));
        Result result = Result.Ok();
        result.put("data", data);
        return result;
    }


}